package indi.kider.server.employee.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Tenant;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.common.model.employee.TenantQueryResponse;
import indi.kider.server.employee.service.TenantService;
import indi.kider.server.employee.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.List;
import java.util.Map;

/**
 * Created by shixj on 2018/4/26.
 */
@RestController
@RequestMapping("tenant")
public class TenantController {
    @Autowired
    private TenantService tenantService;
    @Autowired
    private AuthcService authcService;
    /**
     * @api {GET} /{mainId}/friend
     * @apiGroup tenant
     * @apiDescription 根据id获取租客邀请的好友信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/1/friend
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     */
    @GetMapping("/{mainId}/friend")
    public CommonResponse<List<Tenant>> getTenants(@PathVariable Long mainId) {
        Tenant query = new Tenant();
        query.setMainId(mainId);
        return CommonResponse.buildGetSuccessResponse(tenantService.getTenants(query));
    }

    /**
     * @api {GET} /{id}
     * @apiGroup tenant
     * @apiDescription 根据id获取租客信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     */
    @GetMapping("{id}")
    public CommonResponse<Tenant> getTenantById(@PathVariable Long id) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getTenantById(id));
    }

    /**
     * @api {POST} /list
     * @apiGroup tenant
     * @apiDescription 根据字段查询获取租客列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/list
     * {"num":"0","size":"8","query":{"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}],"total":1,"size":0}}
     */
    @PostMapping("/list")
    public CommonResponse getTenantPages(@RequestBody PageRequest<Tenant> request) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getTenantPages(request));
    }

    /**
     * @api {POST} /list/all
     * @apiGroup tenant
     * @apiDescription 根据字段查询获取租客列表（不分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/list/all
     * {"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}]}
     */
    @PostMapping("/list/all")
    public CommonResponse getTenants(@RequestBody Tenant tenant) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getTenants(tenant));
    }

    /**
     * @api {POST} /
     * @apiGroup tenant
     * @apiDescription 新增租客
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant
     * {"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Tenant tenant) {
        return CommonResponse.buildAddSuccessResponse(tenantService.add(tenant));
    }

    /**
     * @api {PUT} /
     * @apiGroup tenant
     * @apiDescription 修改租客信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant
     * {"id":1,"name":"shixj1","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"name":"shixj1","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody Tenant tenant) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(tenantService.modify(tenant));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {DELETE} /{id}
     * @apiGroup tenant
     * @apiDescription 删除租客
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id) {
        try {
            tenantService.delById(id);
            return CommonResponse.buildDelSuccessResponse("");
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 重置密码
     * @param id
     * @return
     */
    @PostMapping("/resetPassword/{id}")
    public CommonResponse resetPassword(@PathVariable Long id) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            return CommonResponse.buildGetSuccessResponse(tenantService.resetPassword(id,employee));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {GET} /phone/{phone}/rent
     * @apiGroup tenant
     * @apiDescription 租客获取租房信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/phone/18013821856/rent
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"housesId":1,"tenantId":1,"rentDate":null,"state":1,"remark":null,"priceSet":null,"setLease":null,"leaseType":null,"deposit":null,"depositRemark":null,"payCycle":null,"wxopenId":null,"tenantName":null,"tenantTel":null,"houseNumb":null,"building":null,"unit":null,"initWaterRead":null,"initPowerRead":null,"address":null,"community":null}}
     */
    @GetMapping("rent/{key}")
    public CommonResponse getTanantRent(@PathVariable String key) {
        return CommonResponse.buildGetSuccessResponse(tenantService.getTenantRent(key));
    }

    /**
     * @api {POST} /friend
     * @apiGroup tenant
     * @apiDescription 租客邀请好友 mainId：租客的ID（不是好友的）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/friend
     * {"name":"shixj2","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121","mainId":10}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":76,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}}
     */
    @PostMapping("friend")
    public CommonResponse addFriend(@RequestBody Tenant tenant) {
        try {
            return CommonResponse.buildAddSuccessResponse(tenantService.addFriend(tenant));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} rent/list
     * @apiGroup tenant
     * @apiDescription 根据租房信息查询租客列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/rent/list
     * {"num":"0","size":"8","query":{"rentId":"223","name":"shixj2","cardId":12212,"phone":"1221","rentDateStart":"2018-05-15","rentDateEnd":"2018-05-15","expireDateStart":"2018-05-15","expireDateEnd":"2018-05-15"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"rentDate":"2018-05-15","expireDate":"2018-05-15","id":76,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}],"total":1,"size":0}}
     */
    @PostMapping("rent/list")
    public CommonResponse<PageResponse<List<Map>>> getTenantList(@RequestBody PageRequest<TenantQuery> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(tenantService.getTenantList(request)));
    }
    /**
     * @api {POST} rent/list
     * @apiGroup tenant
     * @apiDescription 查询3天未开门的用户（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/getErrInfoList
     * {"num":"0","size":"8","query":{}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"rentDate":"2018-05-15","expireDate":"2018-05-15","id":76,"name":"shixj1","cardId":"12212","phone":"1221","title":null,"nationality":"32","professional":"23","wxopenId":null,"state":1,"type":2,"friendsSize":null,"mainId":69}],"total":1,"size":0}}
     */
    @PostMapping("getErrInfoList")
    public CommonResponse<PageResponse<List<Map>>> getErrInfoList(@RequestBody PageRequest<TenantQuery> request) {
        return CommonResponse.buildGetSuccessResponse(PageUtil.getPageResponse(tenantService.getErrInfoList(request)));
    }
    /**
     * @api {PUT} main/{tenantId}
     * @apiGroup tenant
     * @apiDescription 设置主要承租人
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/tenant/main/{tenantId}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"保存成功！","data":""}
     */
    @PutMapping("main/{tenantId}")
    public CommonResponse<String> setMainTenant(@PathVariable Long tenantId) {
        try {
            tenantService.setMainTenant(tenantId);
            return CommonResponse.buildUpdateSuccessResponse("");
        }catch (Exception e){
            return CommonResponse.buildExceptionResponse(e);
        }

    }



    /**
     * 重置锁密码短信
     * @param tid
     * @return
     */
    @PutMapping("resetLockpwd/{tid}")
    public CommonResponse resetLockpwd(@PathVariable Long tid) {
        try {
            return tenantService.resetLockpwd(tid);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 修改用户 密码数量、卡数量、指纹数量的配置
     * @param tenant
     * @return
     */
    @PostMapping("changeTenantSet")
    public CommonResponse changeTenantSet(@RequestBody Tenant tenant) {
        try {
            return tenantService.changeTenantSet(tenant);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * 每天《早上2点》定时任务,清除本月退租用户密码卡指纹，调用NB接口
     * @return
     */
    @PostMapping("/clearPwdCardFg")
    public CommonResponse clearPwdCardFg(){
        try {
            return tenantService.clearPwdCardFg();
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
